/** 
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
* 
* BaseFlowFileInsService
* 流程文件实例
* 
* 修改纪录
* 2021-01-14 版本：1.0 lishuming 创建。
* @version 版本：1.0
* @author 作者：lishuming
* 创建日期：2021-01-14
*/
package com.topscomm.base.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.base.pojo.BaseFlowFileEntity;
import com.topscomm.base.pojo.BaseFlowFileInsEntity;
import com.topscomm.base.service.IBaseFlowFileInsService;
import com.topscomm.base.service.impl.auto.BaseFlowFileInsServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.ResponseResult;

@Service(value = "baseFlowFileInsService")
@Scope(value = "singleton")
public class BaseFlowFileInsService extends BaseFlowFileInsServiceAuto implements IBaseFlowFileInsService {
	@Autowired
	private BaseFlowFileService BaseFlowFileService;

	/**
	 * 
	 * @description:批量保存文件实例
	 * @auther: wanghanwei
	 * @date: 2021年1月23日下午3:57:07
	 * @modify:
	 */
	@Override
	public ResponseResult<Object> saveBatch(Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		if (paraMap.containsKey("filtersInfo")) {
			Map<String, Object> filtersInfo = (Map<String, Object>) paraMap.get("filtersInfo");
			if (MapUtils.isNotEmpty(filtersInfo)) {
				String flowid = ConvertUtil.convertToString(filtersInfo.get("flow_id"));
				String nodeid = ConvertUtil.convertToString(filtersInfo.get("node_id"));
				String flowinsid = ConvertUtil.convertToString(filtersInfo.get("flow_inst_id"));
				String nodeinsid = ConvertUtil.convertToString(filtersInfo.get("node_inst_id"));
				// 保存到具体的实例表上 采用先删除再插入
				StringBufferProxy sql = new StringBufferProxy();
				sql.appendSingle("{0} = {1}", BaseFlowFileInsEntity.FieldFlowid, flowid);
				sql.appendSingle(" and {0} = {1}", BaseFlowFileInsEntity.FieldNodeid, nodeid);
				sql.appendSingle(" and {0} = {1}", BaseFlowFileInsEntity.FieldFlowinsid, flowinsid);
				sql.appendSingle(" and {0} = {1}", BaseFlowFileInsEntity.FieldNodeinsid, nodeinsid);
				List<BaseFlowFileInsEntity> baseFlowFileInsEntityList = this.queryByWhere(sql.toString());
				if (CollectionUtils.isNotEmpty(baseFlowFileInsEntityList)) {
					this.deleteByWhere(sql.toString());
				}
				StringBufferProxy fileSql = new StringBufferProxy();
				fileSql.appendSingle("{0} = {1}", BaseFlowFileInsEntity.FieldFlowid, flowid);
				fileSql.appendSingle(" and {0} = {1}", BaseFlowFileInsEntity.FieldNodeid, nodeid);
				List<BaseFlowFileEntity> baseFlowFileEntityList = BaseFlowFileService.queryByWhere(fileSql.toString());
				List<BasicEntity> baseFlowFileInsEntityL = new ArrayList<BasicEntity>();
				// 遍历文件添加文件实例
				for (BaseFlowFileEntity baseFlowFileEntity : baseFlowFileEntityList) {
					BaseFlowFileInsEntity baseFlowFileInsEntity = new BaseFlowFileInsEntity();
					long[] arrayId = PrimaryKeyUtil.GeneIDs(baseFlowFileInsEntity.getTableName(),
							baseFlowFileEntityList.size());
					baseFlowFileInsEntity.setId(arrayId[0]);
					baseFlowFileInsEntity.setCode(baseFlowFileEntity.getCode());
					baseFlowFileInsEntity.setName(baseFlowFileEntity.getName());
					baseFlowFileInsEntity.setFlowid(baseFlowFileEntity.getFlowid());
					baseFlowFileInsEntity.setNodeid(baseFlowFileEntity.getNodeid());
					baseFlowFileInsEntity.setFlowinsid(Long.valueOf(flowinsid));
					baseFlowFileInsEntity.setNodeinsid(Long.valueOf(nodeinsid));
					baseFlowFileInsEntity.setUrltype(baseFlowFileEntity.getUrltype());
					baseFlowFileInsEntity.setUrl(baseFlowFileEntity.getUrl());
					baseFlowFileInsEntity.setVersion(baseFlowFileEntity.getVersion());
					baseFlowFileInsEntity.setLastupdatetime(baseFlowFileEntity.getLastupdatetime());
					baseFlowFileInsEntity.setOperatetype(baseFlowFileEntity.getOperatetype());
					baseFlowFileInsEntity.setCreateon(new Date());
					baseFlowFileInsEntity.setModifiedon(new Date());
					baseFlowFileInsEntityL.add(baseFlowFileInsEntity);

				}
				this.insertBatch(baseFlowFileInsEntityL);

			}
			return ResponseResult.ok("操作成功！");

		}
		return ResponseResult.error("查询参数为空");
	}
	
}
